using System;
using System.Collections;
using System.Windows.Forms;
namespace Circus.CodeEditor
{
	public class KeyList 
	{
		internal enum KeyStates
		{
			BookMark = 1,
			Search,
			Outline,
			Select,
			BookMarkEx,
			SelectEx
		}
		private Hashtable states;
		private CodeEditor owner;
		private EventHandlers handlers;
		public KeyData[] EventData
		{
			get
			{
				int num = 0;
				foreach (Hashtable hashtable in this.states.Values)
				{
					num += hashtable.Values.Count;
				}
				KeyData[] array = new KeyData[num];
				int num2 = 0;
				foreach (Hashtable hashtable in this.states.Values)
				{
					foreach (KeyData keyData in hashtable.Values)
					{
						array[num2] = new KeyData(keyData.Keys, keyData.Action, keyData.ActionEx, keyData.Param, keyData.State, keyData.LeaveState);
						num2++;
					}
				}
				return array;
			}
		}
		public EventHandlers Handlers
		{
			get
			{
				return this.handlers;
			}
		}
		private KeyData GetKeyData(Keys keys, int state)
		{
			KeyData keyData = null;
			Hashtable hashtable = (Hashtable)this.states[state];
			if (hashtable != null)
			{
				keyData = (KeyData)hashtable[keys];
			}
			if (keyData == null)
			{
				hashtable = (Hashtable)this.states[-1];
				if (hashtable != null)
				{
					keyData = (KeyData)hashtable[keys];
				}
			}
			return keyData;
		}
		private void Init()
		{
			this.InitStdKeys();
			this.InitWordKeys();
			this.InitCodeCompletionKeys();
			this.InitMovements();
			this.InitSelection();
			this.InitOutlining();
			this.InitBookMarks();
			this.InitSearch();
			this.InitClipboard();
			this.InitMacros();
		}
		private void InitCodeCompletionKeys()
		{
			this.Add(Keys.LButton | Keys.RButton | Keys.MButton | Keys.Space | Keys.Alt, this.handlers.completeWordEvent);
			this.Add(Keys.Space | Keys.Control, this.handlers.completeWordEvent);
			this.Add(Keys.Space | Keys.Shift | Keys.Control, this.handlers.parameterInfoEvent);
			this.Add(Keys.Space | Keys.Shift | Keys.Control, this.handlers.parameterInfoEvent);
			this.Add((Keys)131148, this.handlers.listMembersEvent, 1, 0);
			this.Add(Keys.L, this.handlers.listMembersEvent, 1, 0);
			this.Add((Keys)131145, this.handlers.quickInfoEvent, 1, 0);
			this.Add(Keys.I, this.handlers.quickInfoEvent, 1, 0);
			this.Add((Keys)131160, this.handlers.codeSnippetsEvent, 1, 0);
			this.Add(Keys.X, this.handlers.codeSnippetsEvent, 1, 0);
		}
		private void InitStdKeys()
		{
			this.Add(Keys.Delete, this.handlers.deleteRightEvent);
			this.Add(Keys.Back, this.handlers.deleteLeftEvent);
			this.Add(Keys.Back | Keys.Shift, this.handlers.deleteLeftEvent);
			this.Add(Keys.Back | Keys.Control, this.handlers.deleteWordLeftEvent);
			this.Add(Keys.RButton | Keys.MButton | Keys.Back | Keys.Space | Keys.Control, this.handlers.deleteWordRightEvent);
			this.Add(Keys.Return, this.handlers.newLineEvent);
			this.Add(Keys.LButton | Keys.MButton | Keys.Back | Keys.Control, this.handlers.newLineAboveEvent);
			this.Add(Keys.LButton | Keys.MButton | Keys.Back | Keys.Shift | Keys.Control, this.handlers.newLineBelowEvent);
			this.Add(Keys.LButton | Keys.MButton | Keys.Back | Keys.Shift, this.handlers.newLineEvent);
			this.Add(Keys.Escape, this.handlers.processEscapeEvent);
			this.Add(Keys.Tab, this.handlers.processTabEvent);
			this.Add(Keys.LButton | Keys.Back | Keys.Shift, this.handlers.processShiftTabEvent);
			this.Add(Keys.Back | Keys.Alt, this.handlers.undoEvent);
			this.Add(Keys.Back | Keys.Shift | Keys.Alt, this.handlers.redoEvent);
			this.Add((Keys)131162, this.handlers.undoEvent);
			this.Add((Keys)196698, this.handlers.redoEvent);
			this.Add((Keys)131161, this.handlers.redoEvent);
			this.Add(Keys.Insert, this.handlers.toggleOverwriteEvent);
		}
		private void InitWordKeys()
		{
			this.Add((Keys)131156, this.handlers.charTransposeEvent);
			this.Add((Keys)196692, this.handlers.wordTransposeEvent);
			this.Add((Keys)327764, this.handlers.lineTransposeEvent);
		}
		private void InitBookMarks()
		{
			this.Add((Keys)131147, null, 0, 1);
			this.Add((Keys)131138, null, 0, 5);
			this.Add((Keys)131147, this.handlers.toggleBookMarkEvent, 1, 0);
			this.Add((Keys)131150, this.handlers.nextBookMarkEvent, 1, 0);
			this.Add((Keys)131152, this.handlers.prevBookMarkEvent, 1, 0);
			this.Add(Keys.N, this.handlers.nextBookMarkEvent, 1, 0);
			this.Add(Keys.P, this.handlers.prevBookMarkEvent, 1, 0);
			this.Add(Keys.K, this.handlers.toggleBookMarkEvent, 1, 0);
			this.Add(Keys.N, this.handlers.nextBookMarkEvent, 5, 0);
			this.Add((Keys)131150, this.handlers.nextBookMarkEvent, 5, 0);
			this.Add(Keys.P, this.handlers.prevBookMarkEvent, 5, 0);
			this.Add((Keys)131152, this.handlers.prevBookMarkEvent, 5, 0);
			this.Add(Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.toggleBookMarkEventEx, 0, 1, 0);
			this.Add(Keys.LButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.toggleBookMarkEventEx, 1, 1, 0);
			this.Add(Keys.RButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.toggleBookMarkEventEx, 2, 1, 0);
			this.Add(Keys.LButton | Keys.RButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.toggleBookMarkEventEx, 3, 1, 0);
			this.Add(Keys.MButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.toggleBookMarkEventEx, 4, 1, 0);
			this.Add(Keys.LButton | Keys.MButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.toggleBookMarkEventEx, 5, 1, 0);
			this.Add(Keys.RButton | Keys.MButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.toggleBookMarkEventEx, 6, 1, 0);
			this.Add(Keys.LButton | Keys.RButton | Keys.MButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.toggleBookMarkEventEx, 7, 1, 0);
			this.Add(Keys.Back | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.toggleBookMarkEventEx, 8, 1, 0);
			this.Add(Keys.LButton | Keys.Back | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.toggleBookMarkEventEx, 9, 1, 0);
			this.Add(Keys.D0, this.handlers.toggleBookMarkEventEx, 0, 1, 0);
			this.Add(Keys.D1, this.handlers.toggleBookMarkEventEx, 1, 1, 0);
			this.Add(Keys.D2, this.handlers.toggleBookMarkEventEx, 2, 1, 0);
			this.Add(Keys.D3, this.handlers.toggleBookMarkEventEx, 3, 1, 0);
			this.Add(Keys.D4, this.handlers.toggleBookMarkEventEx, 4, 1, 0);
			this.Add(Keys.D5, this.handlers.toggleBookMarkEventEx, 5, 1, 0);
			this.Add(Keys.D6, this.handlers.toggleBookMarkEventEx, 6, 1, 0);
			this.Add(Keys.D7, this.handlers.toggleBookMarkEventEx, 7, 1, 0);
			this.Add(Keys.D8, this.handlers.toggleBookMarkEventEx, 8, 1, 0);
			this.Add(Keys.D9, this.handlers.toggleBookMarkEventEx, 9, 1, 0);
			this.Add(Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.moveBookMarkEvent, 0);
			this.Add(Keys.LButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.moveBookMarkEvent, 1);
			this.Add(Keys.RButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.moveBookMarkEvent, 2);
			this.Add(Keys.LButton | Keys.RButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.moveBookMarkEvent, 3);
			this.Add(Keys.MButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.moveBookMarkEvent, 4);
			this.Add(Keys.LButton | Keys.MButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.moveBookMarkEvent, 5);
			this.Add(Keys.RButton | Keys.MButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.moveBookMarkEvent, 6);
			this.Add(Keys.LButton | Keys.RButton | Keys.MButton | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.moveBookMarkEvent, 7);
			this.Add(Keys.Back | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.moveBookMarkEvent, 8);
			this.Add(Keys.LButton | Keys.Back | Keys.ShiftKey | Keys.Space | Keys.Control, this.handlers.moveBookMarkEvent, 9);
		}
		private void InitMovements()
		{
			this.Add(Keys.Up, this.handlers.moveLineUpEvent);
			this.Add(Keys.Down, this.handlers.moveLineDownEvent);
			this.Add(Keys.Left, this.handlers.moveCharLeftEvent);
			this.Add(Keys.Right, this.handlers.moveCharRightEvent);
			this.Add(Keys.Prior, this.handlers.movePageUpEvent);
			this.Add(Keys.Next, this.handlers.movePageDownEvent);
			this.Add(Keys.LButton | Keys.Space | Keys.Control, this.handlers.moveScreenTopEvent);
			this.Add(Keys.RButton | Keys.Space | Keys.Control, this.handlers.moveScreenBottomEvent);
			this.Add(Keys.MButton | Keys.Space | Keys.Control, this.handlers.moveFileBeginEvent);
			this.Add(Keys.LButton | Keys.RButton | Keys.Space | Keys.Control, this.handlers.moveFileEndEvent);
			this.Add(Keys.Home, this.handlers.moveLineBeginEvent);
			this.Add(Keys.End, this.handlers.moveLineEndEvent);
			this.Add(Keys.LButton | Keys.MButton | Keys.Space | Keys.Control, this.handlers.moveWordLeftEvent);
			this.Add(Keys.LButton | Keys.RButton | Keys.MButton | Keys.Space | Keys.Control, this.handlers.moveWordRightEvent);
			this.Add(Keys.RButton | Keys.MButton | Keys.Space | Keys.Control, this.handlers.scrollLineUpEvent);
			this.Add(Keys.Back | Keys.Space | Keys.Control, this.handlers.scrollLineDownEvent);
			this.Add((Keys)131293, this.handlers.moveToBraceEvent);
			this.Add((Keys)196829, this.handlers.moveToBraceExtendEvent);
		}
		private void InitOutlining()
		{
			this.Add((Keys)131149, null, 0, 3);
			this.Add((Keys)131148, this.handlers.toggleOutliningEvent, 3, 0);
			this.Add((Keys)131149, this.handlers.toggleOutliningSelectionEvent, 3, 0);
			this.Add((Keys)131151, this.handlers.collapseToDefinitionsEvent, 3, 0);
		}
		private void InitSelection()
		{
			this.Add((Keys)131141, null, 0, 6);
			this.Add(Keys.RButton | Keys.MButton | Keys.Space | Keys.Shift, this.handlers.selectLineUpEvent, SelectionType.Stream);
			this.Add(Keys.Back | Keys.Space | Keys.Shift, this.handlers.selectLineDownEvent, SelectionType.Stream);
			this.Add(Keys.LButton | Keys.MButton | Keys.Space | Keys.Shift, this.handlers.selectCharLeftEvent, SelectionType.Stream);
			this.Add(Keys.LButton | Keys.RButton | Keys.MButton | Keys.Space | Keys.Shift, this.handlers.selectCharRightEvent, SelectionType.Stream);
			this.Add(Keys.LButton | Keys.Space | Keys.Shift, this.handlers.selectPageUpEvent, SelectionType.Stream);
			this.Add(Keys.RButton | Keys.Space | Keys.Shift, this.handlers.selectPageDownEvent, SelectionType.Stream);
			this.Add(Keys.LButton | Keys.Space | Keys.Shift | Keys.Control, this.handlers.selectScreenTopEvent, SelectionType.Stream);
			this.Add(Keys.RButton | Keys.Space | Keys.Shift | Keys.Control, this.handlers.selectScreenBottomEvent, SelectionType.Stream);
			this.Add(Keys.MButton | Keys.Space | Keys.Shift | Keys.Control, this.handlers.selectFileBeginEvent, SelectionType.Stream);
			this.Add(Keys.LButton | Keys.RButton | Keys.Space | Keys.Shift | Keys.Control, this.handlers.selectFileEndEvent, SelectionType.Stream);
			this.Add(Keys.MButton | Keys.Space | Keys.Shift, this.handlers.selectLineBeginEvent, SelectionType.Stream);
			this.Add(Keys.LButton | Keys.RButton | Keys.Space | Keys.Shift, this.handlers.selectLineEndEvent, SelectionType.Stream);
			this.Add(Keys.LButton | Keys.MButton | Keys.Space | Keys.Shift | Keys.Control, this.handlers.selectWordLeftEvent, SelectionType.Stream);
			this.Add(Keys.LButton | Keys.RButton | Keys.MButton | Keys.Space | Keys.Shift | Keys.Control, this.handlers.selectWordRightEvent, SelectionType.Stream);
			this.Add((Keys)131159, this.handlers.selectWordEvent);
			this.Add((Keys)131137, this.handlers.selectAllEvent);
			this.Add((Keys)131153, this.handlers.tabifyEvent);
			this.Add((Keys)196689, this.handlers.unTabifyEvent);
			this.Add((Keys)131157, this.handlers.lowerCaseEvent);
			this.Add((Keys)196693, this.handlers.upperCaseEvent);
			this.Add((Keys)196675, this.handlers.capitalizeEvent);
			this.Add((Keys)131292, this.handlers.deleteWhiteSpaceEvent, 1, 0);
			this.Add((Keys)131139, this.handlers.commentSelectionEvent, 1, 0);
			this.Add((Keys)131157, this.handlers.uncommentSelectionEvent, 1, 0);
			this.Add(Keys.C, this.handlers.commentSelectionEvent, 6, 0);
			this.Add(Keys.C, this.handlers.commentSelectionEvent, 6, 0);
			this.Add((Keys)131139, this.handlers.commentSelectionEvent, 6, 0);
			this.Add(Keys.U, this.handlers.uncommentSelectionEvent, 6, 0);
			this.Add((Keys)131157, this.handlers.uncommentSelectionEvent, 6, 0);
			this.Add(Keys.RButton | Keys.MButton | Keys.Space | Keys.Shift | Keys.Alt, this.handlers.selectLineUpEvent, SelectionType.Block);
			this.Add(Keys.Back | Keys.Space | Keys.Shift | Keys.Alt, this.handlers.selectLineDownEvent, SelectionType.Block);
			this.Add(Keys.LButton | Keys.MButton | Keys.Space | Keys.Shift | Keys.Alt, this.handlers.selectCharLeftEvent, SelectionType.Block);
			this.Add(Keys.LButton | Keys.RButton | Keys.MButton | Keys.Space | Keys.Shift | Keys.Alt, this.handlers.selectCharRightEvent, SelectionType.Block);
			this.Add(Keys.LButton | Keys.Space | Keys.Shift | Keys.Alt, this.handlers.selectPageUpEvent, SelectionType.Block);
			this.Add(Keys.RButton | Keys.Space | Keys.Shift | Keys.Alt, this.handlers.selectPageDownEvent, SelectionType.Block);
			this.Add(Keys.MButton | Keys.Space | Keys.Shift | Keys.Control | Keys.Alt, this.handlers.selectFileBeginEvent, SelectionType.Block);
			this.Add(Keys.LButton | Keys.RButton | Keys.Space | Keys.Shift | Keys.Control | Keys.Alt, this.handlers.selectFileEndEvent, SelectionType.Block);
			this.Add(Keys.MButton | Keys.Space | Keys.Shift | Keys.Alt, this.handlers.selectLineBeginEvent, SelectionType.Block);
			this.Add(Keys.LButton | Keys.RButton | Keys.Space | Keys.Shift | Keys.Alt, this.handlers.selectLineEndEvent, SelectionType.Block);
			this.Add(Keys.LButton | Keys.MButton | Keys.Space | Keys.Shift | Keys.Control | Keys.Alt, this.handlers.selectWordLeftEvent, SelectionType.Block);
			this.Add(Keys.LButton | Keys.RButton | Keys.MButton | Keys.Space | Keys.Shift | Keys.Control | Keys.Alt, this.handlers.selectWordRightEvent, SelectionType.Block);
			this.Add(Keys.LButton | Keys.Space | Keys.Shift | Keys.Control | Keys.Alt, this.handlers.selectScreenTopEvent, SelectionType.Block);
			this.Add(Keys.RButton | Keys.Space | Keys.Shift | Keys.Control | Keys.Alt, this.handlers.selectScreenBottomEvent, SelectionType.Block);
			this.Add((Keys)131154, null, 0, 4);
			this.Add((Keys)131152, this.handlers.swapAnchorEvent, 4, 0);
			this.Add((Keys)131142, this.handlers.formatSelectionEvent, 1, 0);
			this.Add((Keys)131140, this.handlers.formatDocumentEvent, 1, 0);
		}
		private void InitSearch()
		{
			this.Add((Keys)131142, this.handlers.findEvent);
			this.Add(Keys.F3, this.handlers.findNextEvent);
			this.Add((Keys)131186, this.handlers.findNextSelectedEvent);
			this.Add((Keys)65650, this.handlers.findPreviousEvent);
			this.Add((Keys)196722, this.handlers.findPreviousSelectedEvent);
			this.Add((Keys)131144, this.handlers.replaceEvent);
			this.Add((Keys)131143, this.handlers.gotolineEvent);
			this.Add((Keys)131145, this.handlers.initIncrementalSearchEvent);
			this.Add((Keys)196681, this.handlers.initReverseIncrementalSearchEvent);
			this.Add((Keys)262258, null, 0, 2);
			this.Add(Keys.H, this.handlers.toggleHiddenTextEvent, 2, 0);
			this.Add(Keys.C, this.handlers.toggleMatchCaseEvent, 2, 0);
			this.Add(Keys.R, this.handlers.toggleRegularExpressionsEvent, 2, 0);
			this.Add(Keys.B, this.handlers.toggleSearchUpEvent, 2, 0);
			this.Add(Keys.W, this.handlers.toggleWholeWordEvent, 2, 0);
		}
		private void InitClipboard()
		{
			this.Add((Keys)131139, this.handlers.copyEvent);
			this.Add(Keys.LButton | Keys.MButton | Keys.Back | Keys.Space | Keys.Control, this.handlers.copyEvent);
			this.Add((Keys)131160, this.handlers.cutEvent);
			this.Add(Keys.RButton | Keys.MButton | Keys.Back | Keys.Space | Keys.Shift, this.handlers.cutEvent);
			this.Add((Keys)131158, this.handlers.pasteEvent);
			this.Add(Keys.LButton | Keys.MButton | Keys.Back | Keys.Space | Keys.Shift, this.handlers.pasteEvent);
			this.Add((Keys)131148, this.handlers.lineCutEvent);
			this.Add((Keys)196684, this.handlers.lineDeleteEvent);
		}
		private void InitMacros()
		{
			this.Add((Keys)196690, this.handlers.toggleMacroRecording);
			this.Add((Keys)196688, this.handlers.playBackMacro);
		}
		public KeyList(CodeEditor owner)
		{
			this.owner = owner;
			this.handlers = new EventHandlers(owner);
			this.states = new Hashtable();
			this.Init();
		}
		public virtual void Add(Keys keys, KeyEvent action, KeyEventEx actionEx, object param, int state, int leaveState)
		{
			Hashtable hashtable = (Hashtable)this.states[state];
			if (hashtable == null)
			{
				hashtable = new Hashtable();
				this.states.Add(state, hashtable);
			}
			hashtable[keys] = new KeyData(keys, action, actionEx, param, state, leaveState);
		}
		public virtual void Add(Keys keys, KeyEventEx action, object param, int state, int leaveState)
		{
			this.Add(keys, null, action, param, state, leaveState);
		}
		public virtual void Add(Keys keys, KeyEvent action, int state, int leaveState)
		{
			this.Add(keys, action, null, null, state, leaveState);
		}
		public virtual void Add(Keys keys, KeyEventEx action, object param)
		{
			this.Add(keys, null, action, param, -1, 0);
		}
		public virtual void Add(Keys keys, KeyEvent action)
		{
			this.Add(keys, action, null, null, -1, 0);
		}
		public virtual void AddNormal(Keys keys, KeyEventEx action, object param)
		{
			this.Add(keys, null, action, param, 0, 0);
		}
		public virtual void AddNormal(Keys keys, KeyEvent action)
		{
			this.Add(keys, action, null, null, 0, 0);
		}
		public virtual void Remove(Keys keys, int state)
		{
			Hashtable hashtable = (Hashtable)this.states[state];
			if (hashtable != null)
			{
				hashtable.Remove(keys);
			}
		}
		public virtual void Remove(Keys keys)
		{
			this.Remove(keys, -1);
		}
		public virtual void Clear()
		{
			this.states.Clear();
		}
		public virtual bool ExecuteKey(Keys keys, ref int state)
		{
			KeyData keyData = this.GetKeyData(keys, state);
			bool result;
			if (keyData != null)
			{
				if (this.owner.MacroRecording)
				{
					this.owner.RecordKeyData(new MacroKeyData(keyData.Keys, keyData.Action, keyData.ActionEx, keyData.Param, keyData.State, keyData.LeaveState));
				}
				if (keyData.Action != null)
				{
					keyData.Action();
				}
				else
				{
					if (keyData.ActionEx != null)
					{
						keyData.ActionEx(keyData.Param);
					}
				}
				state = keyData.LeaveState;
				result = true;
			}
			else
			{
				result = false;
			}
			return result;
		}
		public virtual bool FindKey(Keys keys, int state)
		{
			return this.GetKeyData(keys, state) != null;
		}
	}
}
